package cn.yshujia.service.impl;

import cn.yshujia.domain.dto.PageDTO;
import cn.yshujia.domain.entity.Comment;
import cn.yshujia.domain.vo.CommentVO;
import cn.yshujia.domain.vo.PageVO;
import cn.yshujia.encryption.AESEncrypt;
import cn.yshujia.enums.MinioFolder;
import cn.yshujia.mapper.CommentMapper;
import cn.yshujia.repository.CommentRepository;
import cn.yshujia.transform.CommentTransform;
import cn.yshujia.utils.IDUtils;
import cn.yshujia.utils.MinioUtils;
import cn.yshujia.utils.PageUtils;
import cn.yshujia.utils.SocketUtils;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author YShuJia
 * @create 2024/4/23
 * @description 服务层
 */

@Slf4j
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements IService<Comment> {
	
	@Resource
	CommentMapper mapper;
	
	@Resource
	CommentRepository repository;
	
	@Resource
	private CommentServiceImpl commentService;
	
	@Resource
	private SocketUtils socketUtils;
	
	@Resource
	UserServiceImpl userService;
	
	public PageVO<CommentVO> selectPage(PageDTO dto) {
		return repository.getPage(dto);
	}
	
	public PageVO<CommentVO> adminPage(PageDTO dto) {
		IPage<Comment> page = mapper.selectPage(new Page<>(dto.getCurrent(), dto.getSize()), null);
		// 创建VO集合
		if (!ObjectUtils.isEmpty(page) && !ObjectUtils.isEmpty(page.getRecords())) {
			return PageUtils.getPage(dto, repository.createVOList(page.getRecords()), page.getTotal());
		}
		return new PageVO<>();
	}
	
	public CommentVO selectById(Long id) {
		if (id != null) {
			Comment comment = mapper.selectById(id);
			if (comment != null) {
				CommentVO commentVO = CommentTransform.entity2VO(comment);
				commentVO.setUser(userService.getUser(comment.getUserId()));
				commentVO.setParentUser(userService.getUser(comment.getParentUserId()));
				return commentVO;
			}
		}
		return null;
	}
	
	@Transactional (rollbackFor = {Exception.class})
	public Long insert(Comment comment) {
		// 评论ID
		comment.setId(IDUtils.getId());
		// 评论来源ID 为空时为 树洞评论 sourceId = id
		if (comment.getSourceId() == null) {
			comment.setSourceId(comment.getId());
			repository.delTree();
		} else {
			repository.delBySourceId(comment.getSourceId());
		}
		// 楼层评论ID 为空时为 自己为楼层评论 floorId = id
		if (comment.getFloorId() == null) {
			comment.setFloorId(comment.getId());
		}
		// 父评论用户ID 为空时为 无回复或回复楼层 parentUserId = userId
		if (comment.getParentUserId() == null) {
			comment.setParentUserId(comment.getUserId());
		}
		if (mapper.insert(comment) > 0) {
			repository.del();
			return comment.getId();
		}
		if (null != comment.getImgUrl()) {
			MinioUtils.delete(comment.getImgUrl());
		}
		return null;
	}
	
	@Transactional (rollbackFor = {Exception.class})
	public boolean insert(MultipartFile file, Comment dto) {
		String url = null;
		if (null != file) {
			url = MinioUtils.upload(file, MinioFolder.COMMENT);
			dto.setImgUrl(url);
		}
		Long id = insert(dto);
		if (null == id) {
			MinioUtils.delete(url);
			return false;
		}
		CommentVO vo = commentService.selectById(id);
		if (null != vo) {
			socketUtils.sendComment(vo);
		}
		return true;
	}
	
	
	@Transactional (rollbackFor = {Exception.class})
	public Boolean remove(List<Long> ids) {
		String data = AESEncrypt.encrypt("该评论已删除");
		int r = mapper.update(new LambdaUpdateWrapper<Comment>()
				.in(Comment::getId, ids)
				.set(Comment::getContent, data));
		// 删除成功，删除缓存
		if (r > 0) {
			repository.del();
		}
		return r > 0;
	}
}
